package com.yiwei.ydd.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.ydd.R;
import com.yiwei.ydd.api.model.VipOrderListModel;
import com.yiwei.ydd.util.UI;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class VipOrderAdapter extends ItemAdapter<VipOrderListModel.DatasBean, VipOrderAdapter.DefaultHolder> {


    private Context context;

    public VipOrderAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_vip_order, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        VipOrderListModel.DatasBean item = getItem(position);
        holder.txtTitle.setText(item.name);
        // 0：待支付，1：已关闭 2：已付款，
        switch (item.status) {
            case "0":
                holder.txtStatus.setText("待付款");
                holder.btnSubmit.setText("去付款");
                holder.btnSubmit.setTextColor(0xffFB5E01);
                holder.btnSubmit.setShapeStrokeColor(0xffFB5E01).setUseShape();
                break;
            case "1":
                holder.txtStatus.setText("交易关闭");
                holder.btnSubmit.setText("删除订单");
                holder.btnSubmit.setTextColor(0xff070707);
                holder.btnSubmit.setShapeStrokeColor(0xffD2D2D2).setUseShape();
                break;
            case "2":
                holder.txtStatus.setText("已付款");
                holder.btnSubmit.setText("油卡充值");
                holder.btnSubmit.setTextColor(0xffFB5E01);
                holder.btnSubmit.setShapeStrokeColor(0xffFB5E01).setUseShape();
                break;
        }
        holder.txtMoney.setText("￥" + item.pay_money);
        Glide.with(context).load(item.icon).into(holder.imgIcon);
        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    onChooseListener.onDetail(item.order_no);
                }
            }
        });
        holder.btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onChooseListener != null) {
                    if (UI.toString(holder.btnSubmit).equals("去付款")) {
                        onChooseListener.onPay(item.pay_money, item.order_no);
                    } else if (UI.toString(holder.btnSubmit).equals("删除订单")) {
                        onChooseListener.onDelete(item.order_no);
                    } else if (UI.toString(holder.btnSubmit).equals("油卡充值")) {
                        onChooseListener.onRecharge();
                    }
                }
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_status)
        TextView txtStatus;
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_title)
        TextView txtTitle;
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.btn_submit)
        SuperButton btnSubmit;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onRecharge();

        void onDelete(String order_no);

        void onPay(String money, String order_no);

        void onDetail(String order_no);
    }
}
